/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package zaire;

import Entity.Phone;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author federicosanchi
 */
public class PhoneMap {
    private static PhoneMap instance;
    private static List<Phone> phoneList;
    private List globalContactList;
    
    private PhoneMap(){
        phoneList = new ArrayList<>();
        globalContactList = new ArrayList<>();
    }
    
    public static PhoneMap getInstance(){
        if (instance == null) instance = new PhoneMap();
        return instance;
    }
    
    public void addPhone(Phone p){
        phoneList.add(p);
        globalContactList.add(p.getPhoneNumber());
    }
    
    public Phone getPhone(int index){
        if(index <= phoneList.size()){
            return phoneList.get(index);
        }
        return null;
    }
    
    public int getPhoneListSize(){
        return phoneList.size();
    }
    
    public void resetPhoneList(){
        phoneList = phoneList = new ArrayList<>();
    }
    
    public Phone getPhoneByNumber(double phoneNumber){
        Phone retPhone = null;
        for(Phone p:phoneList){
            if(p.getPhoneNumber() == phoneNumber){
                retPhone = p;
            }
        }
        return retPhone;
    }
    
    public void fillPhoneLists(double fillingPercentile){
        //int index = 0;
        for(Phone phoneToBeFilled:phoneList){
            //System.out.print("\nTelefono: " + index++);
            for(Object phoneNumber:globalContactList){
                if(Math.random() <= fillingPercentile){
                    phoneToBeFilled.insertPhoneContact((double) phoneNumber);
                    //System.out.print("\ncontatto inserito: " + (double) phoneNumber);
                }
            }
        }
    }
}
